{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6f2d2d00",
   "metadata": {},
   "source": [
    "## Constructing Tasks from Bash Scripts\n",
    "\n",
    "Run a Bash script from within a workflow.\n",
    "\n",
    "### Prerequisites\n",
    "\n",
    "Import `covalent`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dfd92956",
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d2edbbd",
   "metadata": {},
   "source": [
    "### Procedure\n",
    "\n",
    "This example returns the product of two numbers from a function in bash. The possible uses for this technique are endless. In software deployment scenario, for example, you could run a Makefile or a build command on a remote backend machine.\n",
    "\n",
    "1. Define a Covalent `Lepton` object containing the Bash script you want to run."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4bc5c91b",
   "metadata": {},
   "outputs": [],
   "source": [
    "task = ct.Lepton(\n",
    "    language=\"bash\",\n",
    "    command=\"product=$(({num_1} * {num_2}))\",\n",
    "    argtypes=[\n",
    "        (int, ct.Lepton.INPUT_OUTPUT),\n",
    "        (int, ct.Lepton.INPUT_OUTPUT),\n",
    "        (int, ct.Lepton.OUTPUT)\n",
    "    ],\n",
    "    named_outputs=[\"num_1\", \"num_2\", \"product\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccc5ed1d",
   "metadata": {},
   "source": [
    "2. Write a lattice to run the task, then dispatch it and print the result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "83bb70a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(7, 8, 56)\n"
     ]
    }
   ],
   "source": [
    "@ct.lattice\n",
    "def bash_multiply_workflow(**kwargs) -> str:\n",
    "    return task(**kwargs)\n",
    "\n",
    "result = ct.dispatch_sync(bash_multiply_workflow)(num_1=7, num_2=8).result\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19fa4760",
   "metadata": {},
   "source": [
    "The output results from executing the command `product=$((7 * 8))`. The parameter values `num_1` and `num_2` is replaced during execution with the passed values 7 and 8. The `product` variable and operands are then returned as specified in `named_outputs`.\n",
    "\n",
    "The `named_outputs` parameter tells Covalent the environment variables from which to read the script output. You must specify the corresponding types in `argtypes` when constructing the Lepton. In this example, the `num_1`, `num_2`, and `product` variables are all defined as integers."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.15"
  },
  "vscode": {
   "interpreter": {
    "hash": "bc46502ba46b416dacb76460388e15e75b05e952c208e57c8294206d4336b33a"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
